﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ThreeByte.ActiveDeck.Config;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;
using System.Collections.ObjectModel;
using log4net;
using System.ComponentModel;
using ThreeByte.ActiveDeck.Content;

namespace ThreeByte.ActiveDeck.AddIn.Controls.Wizard.Pages
{
    /// <summary>
    /// Interaction logic for CategoryAnswersPage.xaml
    /// </summary>
    public partial class CategoryAnswersPage : Page, INotifyPropertyChanged
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(CategoryAnswersPage));
        private readonly CoreConfig Config;
        private readonly CategoriesConfig pConfig;
        private readonly WorkflowCompleter _workflow;

        public CategoryAnswersPage(string question) {
            InitializeComponent();

            Config = WizardMainFrame.ActiveConfig;
            pConfig = new CategoriesConfig() { Prompt = question };
            pConfig.Background = Config.Theme.Background;
            _workflow = new WorkflowCompleter(this);

            //Bind the categories and words
            _category1 = new Category();
            Category1.DataContext = _category1;

            _category2 = new Category();
            Category2.DataContext = _category2;

            _category3 = new Category();
            Category3.DataContext = _category3;

            _category4 = new Category();
            Category4.DataContext = _category4;

            WordsText.DataContext = pConfig;

            PreviewRoot.Content = PagePreviewFactory.Create(pConfig);
            UpdateCategories();
        }

        private Category _category1;
        private Category _category2;
        private Category _category3;
        private Category _category4;


        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName) {
            if(PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void NextButton_Click(object sender, RoutedEventArgs e) {
            //Bundle up the entered question and create the next page
            NavigationService navService = this.NavigationService;
            if(navService != null) {
                _workflow.Complete();
                navService.Navigate(new QuestionSectionAssignmentPage(pConfig));
            }
        }

        private void CannotExecute(object sender, CanExecuteRoutedEventArgs e) {
            e.CanExecute = false;
            e.Handled = true;
        }

        private void HelpExecuted(object sender, ExecutedRoutedEventArgs e) {

        }

        private void AnswerTextChanged(object sender, TextChangedEventArgs e) {
            UpdateCategories();
        }

        private void UpdateCategories() {
            if(pConfig == null) {
                return;
            }

            pConfig.Categories.Clear();
            if(string.IsNullOrWhiteSpace(Category4.Text)) {
                if(string.IsNullOrWhiteSpace(Category3.Text)) {
                    Category4.IsEnabled = false;
                    if(string.IsNullOrWhiteSpace(Category2.Text)) {
                        Category3.IsEnabled = false;
                        if(string.IsNullOrWhiteSpace(Category1.Text)) {
                            Category2.IsEnabled = false;
                            pConfig.Categories.Add(_category1);
                        } else {
                            Category2.IsEnabled = true;
                            pConfig.Categories.Add(_category1);
                        }
                    } else {
                        Category2.IsEnabled = true;
                        Category3.IsEnabled = true;
                        pConfig.Categories.Add(_category1);
                        pConfig.Categories.Add(_category2);
                    }
                } else {
                    Category2.IsEnabled = true;
                    Category3.IsEnabled = true;
                    Category4.IsEnabled = true;
                    pConfig.Categories.Add(_category1);
                    pConfig.Categories.Add(_category2);
                    pConfig.Categories.Add(_category3);
                }
            } else {
                pConfig.Categories.Add(_category1);
                pConfig.Categories.Add(_category2);
                pConfig.Categories.Add(_category3);
                pConfig.Categories.Add(_category4);
            }

        }

        private void WordsTextChanged(object sender, TextChangedEventArgs e) {
            //pConfig.Words
        }


        
    }
}
